Boolean Expressions

Objectives


Discussion

Boolean Expressions

What are the Values of True and False?

Dim x As Boolean
messagebox.show(cint(x))
x = True
messagebox.show(cint(x))
x = False
messagebox.show(cint(x))

If...then

if (a < b) then messagebox.show("True")

or...

if (a < b) then
    messagebox.show("True")
end if

if x then messagebox.show("True")

MessageBox

Enumerations

Dim x As DialogResult
x = Messagebox.show(...)
if x = DialogResult.Yes then ...

Back to top


Demonstration

1. Open your Unit3 project and add a new form (frmBoolean).

2.  Add a button (btnCheck). 

3.  Create a sub to check Boolean expressions:

Private Sub CheckBoolean()
   messagebox.show(3 < 4 and 5 >= 6)
   messagebox.show(3 < 4 or 5 >= 6)
   messagebox.show(not(3 < 4 and 5 >= 6))
   messagebox.show(3 <> 4 and 5 >= 6)
   messagebox.show(5 < 4 < 6)
   messagebox.show("a" = "A")
   messagebox.show(3 < 4 and 5 >= 6 or 5 < 4)
End Sub

4. Call this sub inside the click event for the button.  Then run the program and check each of the answers.  Experiment with your own expressions until you can determine if the expression will yield True or False.

5. Now we will experiment with if...then.  We will create a function that will compare three numbers to determine if the numbers are in order from lowest to highest:

Private Function InOrder _
(a as int16, b as int16, c as int16) _
as Boolean
    if (a < b and b < c) then
       return(True)
    end if
    return(False)
End Function

6.  In the click event for the button, comment out the call to the Sub.  Add the following two lines which call the function:

MessageBox.Show(InOrder(3, 5, 7))
MessageBox.Show(InOrder(3, 2, 5))

7.  Test the code. Then experiment by passing other numbers into the function InOrder.

8.  We will demonstrate another simple example using a messagebox. Add a button (btnEnd) to your form and set its text property to "Exit". We will use this button to end the program; but we will confirm that the user really wants to exit.  Add the following code to the click event for the button:

Dim answer As DialogResult
answer = MessageBox.Show("Do you want to exit", _
"Exit", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question)
If answer = DialogResult.Yes Then
   End
End If

9.  If you did not type in the code for the messagebox.show statement then you should.  As you type you will be prompted for the next item after each comma and you will be provided with lists of choices.  Notice how many different things you can do with a messagebox.  Also notice that we declared "answer" as DialogResultDialogResult is a special enumerated type that can have only predefined values.  When you type in the line "if answer = " you are provided with a list of acceptable answers based on the type being a DialogResult.  Finally, notice that "End" causes the program to end.

10.  Test your code.  You should be asked if you want to exit.  If you choose "yes" then the program should end; otherwise it won't.

Back to top
Exercises

1.  Given the following:  x = 5, y = 10, z = 20, sName = "bo".  Review each of the following statements and determine if they will evaluate to True or False:

  1. (x < 10)
  2. (x < 10 And y < 10)
  3. (2*x <= y)
  4. Not(z <> x Or z <> x)
  5. (x < 5 Or y < 5 Or z < 5) 
  6. (x*y > y*z And y <= z)
  7. (sName = "bo" Or sName = "Bo")
  8. (sName <> "x")

2.  Create a function that accepts 3 numbers, determines if the numbers  are in reverse order from highest to lowest, and returns True or False. Write a program to test your function.

3.  Create a program that will demonstrate the AND and the OR operators.  Your program should meet the following requirements:

  1. It should have three buttons (btnAND, btnOR, btnExit) and three textboxes (txtTop, txtMiddle, and txtBottom).  The textboxes should be arranged so that txtTop is above txtMiddle, and txtMiddle is above txtBottom.
  2. When the user clicks btnExit, a messagebox should appear to ask the user if she really wants to exit.  If the user clicks "Yes" in the messagebox the program should end. If the user clicks "No" the program should not end.
  3. When the user clicks btnAND or btnOR the program should check if the user has entered something in each textbox.  If the user has not entered something in one of the textboxes, then a messagebox should inform the user to enter numbers in each.  If the user has entered something in each textbox then the checks in parts d and e should be performed.
  4. When the user clicks btnAND the values in the textboxes should be compared to determine if txtTop is greater than txtMiddle AND txtMiddle is greater than txtBottom.  If this condition is true, then a messagebox should appear to indicate True, otherwise the messagebox should indicate False.
  5. When the user clicks btnOR the values in the textboxes should be compared to determine if txtTop is greater than txtMiddle OR txtMiddle is greater than txtBottom.  If this condition is true, then a messagebox should appear to indicate True, otherwise the messagebox should indicate False.
Back to top
Links & Help
Back to top